package com.ctms.system.service.impl;

import com.ctms.common.utils.DateUtils;
import com.ctms.common.utils.ExcelUtils;
import com.ctms.system.dao.TempStudentMapper;
import com.ctms.system.domain.ClassDO;
import com.ctms.system.domain.TempStudentDO;
import com.ctms.system.domain.UserDO;
import com.ctms.system.service.TempStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Service
public class TempStudentServiceImpl implements TempStudentService {

    @Autowired
    TempStudentMapper tempStudentMapper;

    @Override
    public int insertTempStudent(TempStudentDO tempStudentDO) {
        return tempStudentMapper.insertTempStudent(tempStudentDO);
    }


    @Override
    public List<TempStudentDO> selectAllTempStu(Map<String, Object> map) {
        return tempStudentMapper.selectAllTempStu(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return tempStudentMapper.count(map);
    }

    @Override
    public List<UserDO> selectAllUser() {
        return tempStudentMapper.selectAllUser();
    }

    @Override
    public int deleteTempStu(Integer uid) {
        return tempStudentMapper.deleteTempStu(uid);
    }

    @Override
    public List<TempStudentDO> selectAllTemp() {
        return tempStudentMapper.selectAllTemp();
    }

    @Override
    public int countClass(String className) {
        return tempStudentMapper.countClass(className);
    }

    @Override
    public int selectMaxId() {
        return tempStudentMapper.selectMaxId();
    }

    @Override
    public String ajaxUploadExcel(HttpServletRequest request, HttpServletResponse response) {
        //request请求
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        //获取上传的文件
        MultipartFile file = multipartRequest.getFile("upfile");
        //判断文件是否为空
        if(file.isEmpty()){
            try {
                throw new Exception("文件不存在！");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //定义文件输入流
        InputStream in =null;
        try {
            in = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<List<Object>> listob = null;
        try {
            //创建excel文件内容读取对象
            listob = new ExcelUtils().getBankListByExcel(in,file.getOriginalFilename());
        } catch (Exception e) {
            e.printStackTrace();
        }
        //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
        try {
            for (int i = 0; i < listob.size(); i++) {
                List<Object> lo = listob.get(i);
                //判断excel表格里面的表头是否正确
                if (i==0){
                    if (!lo.get(0).equals("学号")){
                        return "文件导入失败";
                    }else if (!lo.get(1).equals("姓名")){
                        return "文件导入失败";
                    }else if(!lo.get(2).equals("性别")){
                        return "文件导入失败";
                    }else if(!lo.get(3).equals("邮箱")){
                        return "文件导入失败";
                    }else if(!lo.get(4).equals("联系电话")){
                        return "文件导入失败";
                    }else if(!lo.get(5).equals("班级")){
                        return "文件导入失败";
                    }
                } else {
                    if (i==1){
                        //导入前先将临时表已有的数据清空
                        tempStudentMapper.deleteAllTempStu();
                    }
                    System.out.println(lo.toString());
                    //定义临时用户对象，并对其赋值
                    TempStudentDO vo = new TempStudentDO();
                    vo.setUsername((String.valueOf(lo.get(0))));
                    vo.setName((String.valueOf(lo.get(1))));
                    String sex = String.valueOf(lo.get(2));
                    int s;
                    //性别转化
                    if (sex.equals("男")){
                        s = 0;
                    }else if (sex.equals("女")){
                        s = 1;
                    }else {
                        s = 2;
                    }
                    vo.setSex(s);
                    vo.setEmail((String.valueOf(lo.get(3))));
                    vo.setMobile((String.valueOf(lo.get(4))));
                    vo.setStudentClass((String.valueOf(lo.get(5))));
                    vo.setIdentity(2);
                    DateUtils dateUtils = new DateUtils();
                    String date = dateUtils.getDate();
                    vo.setGmtCreate(date);
                    vo.setGmtModified(date);
                    vo.setPassword("123456");
                    vo.setStatus("1");
                    //判断上传的用户是否在系统中已存在
                    List<UserDO> userDOS = tempStudentMapper.selectAllUser();
                    for (int j=0;j<userDOS.size();j++){
                        String user_name = userDOS.get(j).getUsername();
                        String name1 = userDOS.get(j).getName();
                        //已存在，标记此学生
                        if (user_name.equals(vo.getUsername()) || (vo.getName()).equals(name1)){
                            vo.setDescription("学生信息已存在");
                            break;
                        }else {
                            vo.setDescription("学生信息上传成功");
                        }
                    }
                    //将上传的学生信息插入临时表
                    tempStudentMapper.insertTempStudent(vo);
                }
            }
            return "文件导入成功！";
        }catch (Exception e){
            return "文件导入失败！";
        }
    }

    @Override
    public int CountByDescription() {
        return tempStudentMapper.CountByDescription();
    }
}
